<?php

/**
*
*/
define('IN_SMART', true);
$smt_root_path = (defined('SMT_ROOT_PATH')) ? SMT_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($smt_root_path . 'common.' . $phpEx);

$channel_id	= request_var('channel_id', 1);
$start = request_var('start', 0);
$start = ($start < 0)? 0 : $start;

// Grab channel profiles
$sql = 'SELECT 
		channel_id, 
		channel_name,
		channel_desc,
		channel_stories_per_page
	FROM ' . CHANNELS_TABLE . ' 
	WHERE channel_id = ' . $channel_id;
$result = $db->sql_query($sql);
$channel_data = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

if (!$channel_data) {
	echo 'The specified channel not exists';
	exit();
}


// get total stories

$sql = 'SELECT COUNT(story_id) AS stories_num
	FROM ' . STORIES_TABLE . ' 
	WHERE story_status = 0 AND channel_id = ' . $channel_id;
$result = $db->sql_query($sql);
$stories_num = $db->sql_fetchrow($result);

$channel_data['stories_num'] = $stories_num['stories_num'];

// Grab story details
$sql = 'SELECT 
		story_id, 
		channel_id, 
		story_title, 
		story_link, 
		story_blurb,
		story_author,
		story_time
	FROM ' . STORIES_TABLE . ' 
	WHERE story_status = 0 AND channel_id = ' . $channel_id . '
	ORDER BY story_time DESC LIMIT ' . $start . ', ' . $channel_data['channel_stories_per_page'];

$result = $db->sql_query($sql);

$stories = array();
while ($row = $db->sql_fetchrow($result))
{
	$row['story_time'] = gmdate('Y-m-d H:i', $row['story_time']);
	$row['story_link'] = $row['story_link'] . '?feedType=RSS';
	$stories[] = $row;
}
$db->sql_freeresult($result);

$template->assign('config', $config);
$template->assign('name', gmdate('Y-m-d H:i O', time()));
$template->assign('channel_data', $channel_data);
$template->assign('stories', $stories);

page_header('', 'application/rss+xml');

$tpl_body = 'rss.xml';

page_footer();

?>